/*
 * Useroutputinterface.h
 *
 *  Created on: Jun 6, 2011
 *      Author: Mario Hock
 */

#ifndef USEROUTPUTINTERFACE_H_
#define USEROUTPUTINTERFACE_H_

#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>

typedef boost::shared_ptr<std::string> StringPtr;

using boost::property_tree::ptree;

class Useroutputinterface
{
public:
    // call virtual implementation
    bool write_to_user(StringPtr s)
    {
        return write_to_user_virtual(s);
    }
    
    // legacy interface  // TODO in cpp Datei?
    bool write_to_user(const std::string& s)
    {
        StringPtr s_ptr(new std::string(s));
        
        return write_to_user_virtual(s_ptr);
    }
    
    // TODO  bool write_to_user(ptree pt) ??
    
    bool write_info(const std::string& s)
    {
        return write_with_tag("info", s);
    }

    bool write_warn(const std::string& s)
    {
        return write_with_tag("info", s);  // TODO "warn"
    }

    // XXX not doing much, but to have it easier in the future..
    bool write_debug(const std::string& s)
    {
        return write_with_tag("info", s);
    }

    
    virtual ~Useroutputinterface() {}
    
    
protected:
    /**
    * @return false on error (e.g. no UI listening)
    */
    virtual bool write_to_user_virtual(StringPtr s) = 0;
    

    
private:
    bool write_with_tag(const std::string& tag, const std::string& s)
    {
        // put message into ptree behind an <info>-tag
        ptree pt;
        pt.put(tag, s);
        
        // write xml to string stream
        std::ostringstream out;
        boost::property_tree::write_xml(out, pt);
        
        // write string stream to string-ptr
        StringPtr s_ptr(new std::string(out.str()));
        
        // hand over to virtual implementation
        return write_to_user_virtual(s_ptr);
    }
};

#endif /* USEROUTPUTINTERFACE_H_ */
